package com.goldcn.config;


import com.goldcn.Interceptor.ErpInterceptor;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.RegistrationBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by Administrator on 2016/3/12.
 */
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ErpInterceptor()).excludePathPatterns ("/sysUser/**","/printTemplate/generateTemplate","/baseClient/getByCode","/account/getByCode","/baseGenerate/getNumberJoint","/swagger-resources","/v2/api-docs");
        super.addInterceptors(registry);
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter("UTF-8",true);
        registrationBean.setFilter(characterEncodingFilter);
        registrationBean.setOrder(RegistrationBean.HIGHEST_PRECEDENCE);
        return registrationBean;
    }

    //spring封装httpcilent的类，带有连接池
    @Bean(name="restTemplate")
    @LoadBalanced
    public RestTemplate restTemplate(){
        ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        return new RestTemplate(clientHttpRequestFactory);
    }

    @Bean(name="smsConfig")
    public SmsConfig smsConfig(){
        return new SmsConfig();
    }

    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedMethods("PUT", "DELETE","POST","GET");
    }

}
